package gate;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import client.remote.ClientReceiver;

public class ServerPackage implements Serializable
{
	private static ServerPackage instance;
	private Map<Long,ClientReceiver> clientsConnected = new TreeMap<Long,ClientReceiver>();
	private Map<Integer, ArrayList<Long>> rooms = new TreeMap<Integer,ArrayList<Long>>();
	private int roomCounter = -1;
	private long userIdCounter = 0;
	private ServerPackage() {}
	public synchronized static ServerPackage getServerPackage()
	{
		if(instance == null)
		{
			instance = new ServerPackage();
		}
		return instance;
	}
	public void addClient(Long clientId,ClientReceiver client)
	{
		if(clientId > userIdCounter) userIdCounter = clientId;
		clientsConnected.put(clientId, client);
	}
	public void removeClient(Long clientId)
	{
		clientsConnected.remove(clientId);
	}
	public int getRoomCounter() { return roomCounter; }
	public void createRoom(Integer roomId,ArrayList<Long> clientsInRoom)
	{
		if(roomId > roomCounter) roomCounter = roomId;
		rooms.put(roomId, clientsInRoom);
	}
	public void clientLeftRoom(Integer roomId,Long clientId)
	{
		rooms.get(roomId).remove(clientId);
	}
	public void gameEnded(Integer roomId)
	{
		rooms.remove(roomId);
	}
	public Map<Long,ClientReceiver> getClients()
	{
		return clientsConnected;
	}
	public Map<Integer,ArrayList<Long>> getRooms()
	{
		return rooms;
	}
	public long getClientCounter()
	{
		return userIdCounter;
	}
}
